package com.xiaoye.qbproject.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoye.qbproject.domain.CountryRegion;
import com.xiaoye.qbproject.domain.ZbInfo;
import com.xiaoye.qbproject.domain.ZbRelation;
import com.xiaoye.qbproject.domain.ZbType;
import com.xiaoye.qbproject.dto.NodeAndRelationDto;
import com.xiaoye.qbproject.mapper.*;
import com.xiaoye.qbproject.service.BlbcTroopsService;
import com.xiaoye.qbproject.service.QbWarfareExamplesService;
import com.xiaoye.qbproject.service.RelationService;
import com.xiaoye.qbproject.service.ZbInfoService;
import com.xiaoye.qbproject.vo.relation.Line;
import com.xiaoye.qbproject.vo.relation.Node;
import com.xiaoye.qbproject.vo.relation.RelationVo;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : ChenYao
 * @version : 1.0
 * @description :
 * @create : 2025-01-06 11:52
 */
@Slf4j
@Service
public class RelationServiceImpl implements RelationService {
    @Autowired
    private ZbInfoService zbInfoService;
    @Autowired
    private ZbInfoMapper zbInfoMapper;
    @Autowired
    private ZbRelationMapper zbRelationMapper;
    @Autowired
    private BlbcTroopsMapper blbcTroopsMapper;
    @Autowired
    private BlbcZbRelationMapper blbcZbRelationMapper;
    @Autowired
    private BlbcWarfareExamplesRelationMapper blbcWarfareExamplesRelationMapper;
    @Autowired
    private QbWarfareExamplesMapper qbWarfareExamplesMapper;
    @Autowired
    private BlbcTroopsService blbcTroopsService;
    @Autowired
    private QbWarfareExamplesService qbWarfareExamplesService;
    @Autowired
    private CountryRegionMapper countryRegionMapper;
    @Autowired
    private ZbTypeMapper zbTypeMapper;

    /**
     * @param entityName 实体名称
     * @description: 获取情报图谱 实体详情
     * @author: ChenYao
     * @return: List<RelationVo>
     * @ceateDate: 2025-01-06 11:54
     */
    @Override
    public RelationVo getQbRelation(String entityName) {
        // 返回
        RelationVo relationVo = new RelationVo();
        List<String> zbFromIds = new ArrayList<>();


        List<String> blbcIds = new ArrayList<>();


        // 根据 实体名称 搜索 战例
        List<NodeAndRelationDto> warfareExamplesHaveRelation = blbcWarfareExamplesRelationMapper.getWarfareExamplesHaveRelation(entityName);
        // 战例ids
        List<String> warfareExampleIds = new ArrayList<>();
        warfareExamplesHaveRelation.forEach(nodeAndRelationDto -> {
            relationVo.getNodes().add(new Node(nodeAndRelationDto.getId(), nodeAndRelationDto.getName(), RelationVo.NodeTypeEnum.ZL));
            relationVo.getLines().add(new Line(nodeAndRelationDto.getFromId(), nodeAndRelationDto.getToId(), nodeAndRelationDto.getText(), RelationVo.NodeTypeEnum.ZL));
            blbcIds.add(nodeAndRelationDto.getFromId());
            warfareExampleIds.add(nodeAndRelationDto.getToId());
        });
        if (!Collections.isEmpty(warfareExampleIds)) {
            qbWarfareExamplesMapper.getQbWarfareExamplesNodesByFromIds(warfareExampleIds).forEach(node -> {
                        node.setType(RelationVo.NodeTypeEnum.ZL);
                        relationVo.getNodes().add(node);
                    }
            );
        }

        // 根据 实体名称 搜索 兵力
        List<NodeAndRelationDto> blbcHaveRelation = blbcZbRelationMapper.getBlbcHaveRelation(entityName);
        // 增加被指向的节点信息 及 连接线信息
        blbcHaveRelation.forEach(nodeAndRelationDto -> {
            relationVo.getNodes().add(new Node(nodeAndRelationDto.getId(), nodeAndRelationDto.getName(), RelationVo.NodeTypeEnum.BLBC));
            relationVo.getLines().add(new Line(nodeAndRelationDto.getToId(), nodeAndRelationDto.getFromId(), nodeAndRelationDto.getText(), RelationVo.NodeTypeEnum.BLBC));
            zbFromIds.add(nodeAndRelationDto.getFromId());
            blbcIds.add(nodeAndRelationDto.getToId());

        });
        if (!Collections.isEmpty(blbcIds)) {
            // 获取起始节点信息
            blbcTroopsMapper.getBlbcNodesByFromIds(blbcIds).forEach(node -> {
                node.setType(RelationVo.NodeTypeEnum.BLBC);
                relationVo.getNodes().add(node);
            });
        }


        // 获取有关系的节点信息
        List<NodeAndRelationDto> zbHaveRelation = zbInfoMapper.getZbHaveRelation(entityName);
        //List<String> zbToIds = new ArrayList<>();

        // 增加被指向的节点信息 及 连接线信息
        zbHaveRelation.forEach(nodeAndRelationDto -> {
            relationVo.getNodes().add(new Node(nodeAndRelationDto.getId(), nodeAndRelationDto.getName(), RelationVo.NodeTypeEnum.ZB));
            relationVo.getLines().add(new Line(nodeAndRelationDto.getToId(), nodeAndRelationDto.getFromId(), nodeAndRelationDto.getText(), RelationVo.NodeTypeEnum.ZB));
            zbFromIds.add(nodeAndRelationDto.getFromId());
            zbFromIds.add(nodeAndRelationDto.getToId());

        });
        if (!Collections.isEmpty(zbFromIds)) {
            // 获取起始节点信息
            List<Node> fromNode = zbInfoMapper.getZbNodesByFromIds(zbFromIds);
            fromNode.forEach(node -> {
                node.setType(RelationVo.NodeTypeEnum.ZB);
                relationVo.getNodes().add(node);
            });
        }


        //relationVo.getNodes().addAll(blbcTroopsMapper.getBlbcNodesByFromIds(blbcIds));


        return relationVo;
    }

    @Override
    public RelationVo getQbRelationAll(String entityName) {
        // 返回
        RelationVo relationVo = new RelationVo();

        // 根据 实体名称 搜索 装备
        List<Node> zbNodes = zbInfoMapper.getZbNodes(entityName, null);
        List<String> zbFromIds = new ArrayList<>();
        zbNodes.forEach(zbNode -> {
            zbNode.setType(RelationVo.NodeTypeEnum.ZB);
            zbFromIds.add(zbNode.getId());
        });
        relationVo.getNodes().addAll(zbNodes);

        // 获取子节点装备信息
        List<Node> subNodes = zbInfoMapper.getZbNodesByFromIds(zbFromIds);
        subNodes.forEach(zbNode -> zbNode.setType(RelationVo.NodeTypeEnum.ZB));
        // 添加到 node中
        relationVo.getNodes().addAll(subNodes);
        // 获取关系信息
        List<ZbRelation> zbRelations = zbRelationMapper.selectList(Wrappers.<ZbRelation>query().in("from_id", zbFromIds));
        List<Line> zbLines = zbRelations.stream().map(zbRelation -> new Line(zbRelation.getFromId(), zbRelation.getToId(), zbRelation.getText(), RelationVo.NodeTypeEnum.ZB)).collect(Collectors.toList());
        relationVo.getLines().addAll(zbLines);


        // 根据 实体名称 搜索 兵力


        // 根据 实体名称 搜索 战例
        return relationVo;
    }

    @Override
    public RelationVo getQbRelationAll2(String entityName, String countryIds) {
        // 返回
        RelationVo relationVo = new RelationVo();

        List<ZbInfo> zbInfos = zbInfoMapper.selectList(Wrappers
                .<ZbInfo>lambdaQuery()
                .like(StringUtils.isNotBlank(entityName), ZbInfo::getName, entityName)
                .in(StringUtils.isNotBlank(countryIds), ZbInfo::getSourceCountryId, countryIds.split(","))
        );
        if (Collections.isEmpty(zbInfos)) {
            log.info("没有查询到装备信息");
            return relationVo;
        }
        // 节点集和
        Set<Node> nodes = relationVo.getNodes();
        // 关系集和
        Set<Line> lines = relationVo.getLines();
        // 查询武器
        Set<String> zbTypeId = new HashSet<>();
        Set<String> countryId = new HashSet<>();
        zbInfos.forEach(zbInfo -> {
            zbTypeId.add(zbInfo.getType());
            countryId.add(zbInfo.getSourceCountryId());

            // 添加节点信息
            nodes.add(new Node(zbInfo.getId(), zbInfo.getName(), RelationVo.NodeTypeEnum.ZB));
            // 添加关系信息
            lines.add(new Line(zbInfo.getSourceCountryId(), zbInfo.getType(), "拥有", RelationVo.NodeTypeEnum.GJ));
            lines.add(new Line(zbInfo.getType(), zbInfo.getId(), "部署", RelationVo.NodeTypeEnum.ZB));
        });

        // 查询国家列表
        countryRegionMapper.selectList(Wrappers.<CountryRegion>query().in("id", countryId)).forEach(country -> {
            // 添加节点信息
            nodes.add(new Node(country.getId(), country.getName(), RelationVo.NodeTypeEnum.GJ));
        });
        // 查询装备类型列表
        zbTypeMapper.selectList(Wrappers.<ZbType>query().in("id", zbTypeId)).forEach(zbType -> {
            // 添加节点信息
            nodes.add(new Node(zbType.getId(), zbType.getName(), RelationVo.NodeTypeEnum.ZBTYPE));
        });


        return relationVo;
    }
}
